<template>
  <el-card class="box-card">
    <!--头部 start-->
    <template #header>
      <div class="card-header">
        <h3>
          <el-icon style="margin-right: 10px"><Platform /></el-icon>互联网IP管理
        </h3>

        <!--搜索区域 start-->
        <div class="card-search">
          <el-row :gutter="10">
            <el-col :span="11">
              <el-input
                prefix-icon="Search"
                v-model="searchValue"
                @keyup.enter.native="search"
                placeholder="关键字搜索（回车）"
              />
            </el-col>
            <el-col :span="10">
              <div class="my-button">
                <el-button
                  plain
                  style="width: 50%"
                  color="#076613"
                  @click="exportExcelAction"
                >
                  <el-icon><Download /></el-icon>
                  导出Excel
                </el-button>
              </div>
            </el-col>

            <el-col
              :span="3"
              style="
                display: inline-flex;
                justify-content: center;
                align-items: center;
                cursor: pointer;
              "
            >
              <el-icon style="font-size: 20px; color: #b3b6bc" @click="refresh"
                ><Refresh
              /></el-icon>
            </el-col>
          </el-row>
        </div>
        <!--搜索区域 end-->
      </div>
    </template>
    <!--头部 end-->

    <!--表格区域 start-->
    <div class="table-box">
      <el-table
        element-loading-text="数据加载中..."
        v-loading="loading"
        :data="tableData"
        style="width: 100%; text-align: center"
        :cell-style="{ textAlign: 'center' }"
        :header-cell-style="{
          fontSize: '15px',
          background: '#e99d53',
          color: 'white',
          textAlign: 'center',
        }"
      >
        <el-table-column
          label="序号"
          width="100"
          type="index"
          :index="Nindex"
        />

        <el-table-column label="IP">
          <template #default="scope">
            <span>{{ scope.row.ip_address }}</span>
          </template>
        </el-table-column>

        <el-table-column label="使用人">
          <template #default="scope">
            <span>{{ scope.row.user }}</span>
          </template>
        </el-table-column>

        <el-table-column label="部门">
          <template #default="scope">
            <span>{{ scope.row.department }}</span>
          </template>
        </el-table-column>
        <el-table-column label="MAC">
          <template #default="scope">
            <span>{{ scope.row.mac_address }}</span>
          </template>
        </el-table-column>
        <el-table-column label="硬盘序列号">
          <template #default="scope">
            <span>{{ scope.row.disk_sn }}</span>
          </template>
        </el-table-column>
        <el-table-column label="设备类型">
          <template #default="scope">
            <span>{{ scope.row.pc_type }}</span>
          </template>
        </el-table-column>
        <el-table-column label="设备位置">
          <template #default="scope">
            <span>{{ scope.row.position }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作系统">
          <template #default="scope">
            <span>{{ scope.row.operate_system }}</span>
          </template>
        </el-table-column>
        <el-table-column label="品牌">
          <template #default="scope">
            <span>{{ scope.row.brand }}</span>
          </template>
        </el-table-column>

        <el-table-column label="更新时间">
          <template #default="scope">
            <span>{{ formatTime(scope.row.updated_at, "yyyy-MM-dd") }}</span>
          </template>
        </el-table-column>

        <el-table-column fiexed="right" width="50" label="操作">
          <template #default="scope">
            <el-button size="small" @click="editUser(scope.row.id)"
              ><el-icon><Edit /></el-icon>编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!--分页插件 start-->
      <el-pagination
        background
        layout="total,sizes,prev,pager,next,jumper"
        :total="total"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30, 40]"
        @current-change="changePage"
      />
      <!--分页插件 end-->
    </div>
    <!--表格区域 end-->
    <!--编辑管理员弹出框 start-->
    <el-dialog
      v-model="editUserVisible"
      align-center
      width="42%"
      destroy-on-close
    >
      <template #header>
        <div class="my-header">
          <el-icon size="26px"><EditPen /></el-icon>
          <h1>编辑管理员</h1>
        </div>
      </template>
      <!--编辑管理员组件 start-->
      <EditUser
        :userInfo="userInfo"
        @closeEditUserForm="closeEditUserForm"
        @success="success"
      />
      <!--编辑管理员组件 end-->
    </el-dialog>
    <!--编辑管理员弹出框 end-->
  </el-card>
</template>

<script setup lang="ts">
import { ref, reactive, toRefs, onMounted } from "vue";
import { getUserApi } from "@/api/system/user/user";
import { getIpPublicListApi } from "@/api/ip/ippublic/ippublic";
import { formatTime } from "@/utils/date";
import { ElMessage } from "element-plus";
import EditUser from "@/views/system/user/components/EditUser.vue";
import { exportExcel } from "@/utils/exportExcel";

// 服务器路径
const url = import.meta.env.VITE_APP_BASE_API;
const state = reactive({
  // 搜索关键字
  searchValue: "",
  // 表格数据
  tableData: [],
  // 总条数
  total: 0,
  // 每页显示行数
  pageSize: 10,
  // 当前页码
  pageIndex: 1,
  // 数据加载
  loading: false,
});

// 获取互联网IP列表数据
const loadData = async (state: any) => {
  state.loading = true;
  // 先清空表格数据
  state.tableData = [];
  // 查询参数
  const params = {
    pageIndex: state.pageIndex,
    pageSize: state.pageSize,
    keyword: state.searchValue,
  };
  const { data } = await getIpPublicListApi(params);
  state.tableData = data.result.list;
  state.total = data.result.count;
  state.loading = false;
};

// 刷新
const refresh = () => {
  // 搜索关键字
  state.searchValue = "";
  // 加载数据
  loadData(state);
};

// 搜索
const search = () => {
  if (state.searchValue != null && state.searchValue != "") {
    loadData(state);
    ElMessage({
      type: "success",
      message: `关键字“${state.searchValue}”搜索内容如下`,
    });
  }
};

// 切换页码执行事件
const changePage = (val: number) => {
  state.pageIndex = val;
  loadData(state);
};

// 处理列表序号
const Nindex = (index: number) => {
  // 当前页码
  const page = state.pageIndex;
  // 每页行数
  const pageSize = state.pageSize;
  return index + 1 + (page - 1) * pageSize;
};

// 提交表单后回调函数
const success = () => {
  closeEditUserForm();
  loadData(state);
};

// 20240119
// 编辑管理员弹出框状态
const editUserVisible = ref(false);
// 管理员信息
const userInfo = ref();
// 获取管理员信息
const editUser = async (id: number) => {
  const { data } = await getUserApi(id);
  userInfo.value = data.result;
  editUserVisible.value = true;
};
// 关闭编辑管理员弹出框
const closeEditUserForm = () => {
  editUserVisible.value = false;
};

// 定义需要导出的列名对象
const column = [
  { name: "id", label: "用户ID" },
  { name: "username", label: "用户名称" },
  { name: "phone", label: "手机号" },
  { name: "email", label: "邮箱" },
  { name: "remarks", label: "备注" },
];

// 导出excel函数
const exportExcelAction = () => {
  exportExcel({
    column,
    data: state.tableData,
    filename: "管理员信息数据",
    format: "xlsx",
    autowidth: true,
  });
};

// 挂载后加载列表数据
onMounted(() => {
  loadData(state);
});
const { tableData, pageSize, loading, total, searchValue } = toRefs(state);
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #e99d53;
}
.card-header h3 {
  display: inline-flex;
  justify-content: center;
  align-items: center;
}

.el-card {
  border-radius: 0px;
  border: none;
}
/*自定义按钮样式*/
.my-button {
  display: flex;
  justify-content: space-between;
}

/*自定义弹出框头部样式*/
.my-header {
  display: flex;
  justify-content: flex-start;
  color: #e99d53;
}

/*自定义分页样式*/
.el-pagination {
  margin-top: 20px;
  justify-content: center;
}
:deep(.el-pagination.is-background .el-pager li:not(.is-disabled).is-active) {
  background-color: #e99d53;
}
/*修改v-loading样式*/
:deep(.el-loading-spinner .el-loading-text) {
  color: #e99d53;
}
:deep(.el-loading-spinner .path) {
  stroke: #e99d53;
}
</style>
